package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actgradefund;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.Map;

/**
 * 成长基金
 * @author Dell
 *
 */
public class ActiveModule_成长基金 extends PlayerModule {

	//bean -------------------------------
	public static final int acttype = 106;
	
	private DateTime buytime;
	private ArrayList<Integer> getids;
	
	public DateTime getBuytime() {
		return buytime;
	}
	public void setBuytime(DateTime buytime) {
		this.buytime = buytime;
	}
	public ArrayList<Integer> getGetids() {
		return getids;
	}
	public void setGetids(ArrayList<Integer> getids) {
		this.getids = getids;
	}
	
	//func ---------------------------------
	
	private static ActiveModule_成长基金 create(String playerCode) {
		ActiveModule_成长基金 am = new ActiveModule_成长基金();
		am.setPlayerCode(playerCode);
		am.setBuytime(new DateTime(0));
		am.setGetids(new ArrayList<Integer>());
		am.set();
		return am;
	}
	
	public void handlebuy() {
		setBuytime(DateTime.now());
		this.set();
	}
	
	public void adddrawid(int id) {
		getGetids().add(id);
		this.set();
	}
	
//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
	
	// redis ---------------------------------------------------
	
	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public ActiveModule_成长基金 get(String playerCode) {
		try {
			ActiveModule_成长基金 module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_成长基金.class);
				if(module != null){module.set();}//同步到redis

				if (module == null) {
					module = create(playerCode);
				}
				if(module == null){return null;}
			}

			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
		
		if(!getBuytime().isEqual(new DateTime(0))) {
			Map<String, actgradefund> agfs = ActDataCenter.getAllData(actgradefund.class, getPlayerCode());
			ArrayList<Integer> getidlist = getGetids();
			
			if(agfs.size() == getidlist.size()) {
				new TipHandle().updateTip(getPlayerCode(), TipType.活动_成长基金奖励, false);
				return;
			}
			
			PlayerBean pb = PlayerBean.get(getPlayerCode());
			int level = pb.getLevel();
			
			for(String key : agfs.keySet()) {
				actgradefund agf = agfs.get(key);
				int donum = agf.getDonum();
				if(level >= donum) {
					int id = agf.getId();
					if(!getidlist.contains(id)) {
						new TipHandle().updateTip(getPlayerCode(), TipType.活动_成长基金奖励, true);
						return;
					}
				}
			}
		}
		
		new TipHandle().updateTip(getPlayerCode(), TipType.活动_成长基金奖励, false);
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_成长基金 activeModule_成长基金 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_成长基金);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}
}
